# -*- coding: utf-8 -*-
"""
#
# Authors: limanman
# OsChina: http://my.oschina.net/pydevops/
# Purpose:
#
"""
import MySQLdb as mdb
from MySqlPool import Mysql
import datetime
import time
import traceback
from datetime import datetime, timedelta
from logger import logger

from select import select
from socket import socket
from socket import AF_INET, SOCK_STREAM

import sys
reload(sys)
sys.setdefaultencoding('utf8')




from recv_packet import RecvPacket
from opccode import handlePacket
#s.send(sendData)
#s.recv(1024)
class TcpClient:

    def __init__(self,host='localhost', port=6800):
        self.connectSocket=socket(AF_INET, SOCK_STREAM)
        self.connectSocket.setsockopt(0)
        self.connectSocket.connect((host,port))
        self.recvData = ''

    def recvData(self):
        reads,_,errors = select([self.connectSocket],[],[],0.0001)
        if self.connectSocket in reads:
            return self.connectSocket.recv(1024)
            
    def sendData(self, databuf):
        self.connectSocket.send(databuf)

def main():
    """Main function."""
    mysql = Mysql()
    lampaddr = '00000188'
    sql = "select status,ip,port from network where is = 7506"
    info_list = mysql.getAll(sql)
    print str(info_list)
    if len(info_list)<=0:
        return
    
    network = info_list[0]
    
    if network.status == 0:
        return
    netClient = NetClient(network.ip, int(network.prot))
    netClient.sendData('hello gospider');
    print netClient.recvData();

if __name__ == '__main__':
    main()
